package com.meeting.master.entrance.web.query.executor;

import com.meeting.master.entrance.web.converter.RoomInfoVoConverter;
import com.meeting.master.entrance.web.query.assembler.RoomInfoVoDataAssembler;
import com.meeting.master.entrance.web.vo.RoomInfoVo;
import com.meeting.master.manager.converter.RoomInfoDtoConverter;
import com.meeting.master.manager.dto.RoomInfoBaseDto;
import com.meeting.master.manager.dto.RoomInfoDto;
import com.meeting.master.persist.qto.GetRoomInfoQto;
import com.meeting.master.service.RoomInfoBaseDtoService;
import com.meeting.master.service.index.entity.GetRoomInfoQtoService;
import com.vs.code.AutoGenerated;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/** RoomInfoVo查询方案入口 */
@Service
@AutoGenerated(locked = false, uuid = "5a6e049d-1329-3709-9283-4029890ff90d")
public class RoomInfoVoQueryExecutor {
    @AutoGenerated(locked = true)
    @Resource
    private GetRoomInfoQtoService getRoomInfoQtoService;

    @AutoGenerated(locked = true)
    @Resource
    private RoomInfoBaseDtoService roomInfoBaseDtoService;

    @AutoGenerated(locked = true)
    @Resource
    private RoomInfoDtoConverter roomInfoDtoConverter;

    @AutoGenerated(locked = true)
    @Resource
    private RoomInfoVoConverter roomInfoVoConverter;

    @AutoGenerated(locked = true)
    @Resource
    private RoomInfoVoDataAssembler roomInfoVoDataAssembler;

    /** 根据GetRoomInfoQto查询RoomInfoVo列表,上限500 */
    @AutoGenerated(locked = false, uuid = "b896b13e-f34d-3f18-a922-0a581ce2888d")
    public List<RoomInfoVo> getRoomInfo(@NotNull GetRoomInfoQto qto) {
        /** This block is generated by vs, do not modify, start anchor 1 */
        List<Long> ids = getRoomInfoQtoService.query(qto);
        Map<Long, RoomInfoVo> idVoMap = toIdVoMap(ids);
        roomInfoVoDataAssembler.assembleData(idVoMap);
        List<RoomInfoVo> result =
                ids.stream()
                        .map(id -> idVoMap.get(id))
                        .filter(Objects::nonNull)
                        .collect(Collectors.toList());
        /** This block is generated by vs, do not modify, end anchor 1 */
        // 自定义逻辑,e.g. 数据过滤、重组、批量处理等

        /** This block is generated by vs, do not modify, start anchor 2 */
        return result;
        /** This block is generated by vs, do not modify, end anchor 2 */
    }

    /** 将ID列表转换为Map<ID, VO> */
    @AutoGenerated(locked = true, uuid = "ec6f429f-5276-3820-b454-3046411e8e16")
    private Map<Long, RoomInfoVo> toIdVoMap(List<Long> ids) {
        List<RoomInfoBaseDto> rootBaseDtoList = roomInfoBaseDtoService.getByIds(ids);
        Map<Long, RoomInfoBaseDto> baseDtoMap =
                rootBaseDtoList.stream()
                        .collect(
                                Collectors.toMap(
                                        RoomInfoBaseDto::getId,
                                        Function.identity(),
                                        (o1, o2) -> o1,
                                        LinkedHashMap::new));
        Map<Long, RoomInfoDto> dtoMap =
                roomInfoDtoConverter
                        .convertFromRoomInfoBaseDtoToRoomInfoDto(
                                new ArrayList<>(baseDtoMap.values()))
                        .stream()
                        .collect(Collectors.toMap(RoomInfoDto::getId, Function.identity()));
        Map<RoomInfoDto, RoomInfoVo> voMap =
                roomInfoVoConverter.convertToRoomInfoVoMap(new ArrayList<>(dtoMap.values()));
        Map<Long, RoomInfoVo> idVoMap =
                baseDtoMap.values().stream()
                        .collect(
                                Collectors.toMap(
                                        RoomInfoBaseDto::getId,
                                        baseDto -> voMap.get(dtoMap.get(baseDto.getId())),
                                        (o1, o2) -> o1,
                                        LinkedHashMap::new));
        return ids.stream()
                .collect(
                        Collectors.toMap(
                                Function.identity(),
                                id -> idVoMap.get(id),
                                (o1, o2) -> o1,
                                LinkedHashMap::new));
    }
}
